If Statements
If statements allow you to define statements or groups of statements that are executed only in specific circumstances. Each If statement contains one or more Boolean expressions whose values can be eithertrue
orfalse
. AppleScript executes the statements contained in the If statement only if the value of the Boolean expression istrue
.If statements are also called conditional statements. Boolean expressions in If statements are also called tests.
The following example uses an If statement to control whether or not a particular dialog box is displayed:
if dependents > 2 then display dialog "You might need to file an extra form"end ifThe If statement contains the Boolean expressiondependents > 2
. If the value of the Boolean expression istrue
, the Display Dialog command is executed. If the value of the Boolean expression isfalse
, the Display Dialog command is not executed. (Display Dialog is a scripting addition command. For more information about the way it works, see the AppleScript Scripting Additions Guide.)If statements can contain multiple tests. For example, the following statement contains three tests.
if ( x > y ) then set myMessage to " is greater than "else if ( x < y ) then set myMessage to " is less than "else set myMessage to " is equal to "end if set myResult to (x as string) & myMessage & (y as string)If the expressionx > y
istrue
, the value of the variablemyMessage
is set to " is greater than " and the If statement is finished. Control passes to the Set statement, which uses the value of the variablemyMessage
to set the value of another variable, calledmyResult
. The value ofmyResult
is a string such as "7 is greater than 5". If the first Boolean expression isfalse
, the next expression,x < y
, is evaluated with similar results.An If statement can contain any number of Else If clauses; AppleScript looks for the first Boolean expression contained in an If or Else If clause that is
true
, executes the statements contained in its block (the statements between one Else If and the following Else If or Else clause), and then exits the If statement.An If statement can also include a final Else clause. The statements in its block are executed if no other test in the If statement passes. For example, suppose the values of
x
andy
in the previous example are both 112. The first two tests,x > y
andx < y,
fail. The value of the variablemyMessage
is set to " is equal to ", and the value ofmyResult
is "112 is equal to 112".If statements can be more elaborate, as in this example:
display dialog "How many dependents?" default answer ""set dependents to (text returned of result) as integer display dialog "Have you ever been audited?" buttons ¬ {"No", "Yes"} if button returned of result = "Yes" then set audit to true else set audit to false end if if dependents < 9 and audit = false then display dialog "No extra forms are required."else if dependents < 9 and audit = true then display dialog "You might need to file an extra form."else --anything greater than 9 display dialog "You will need to file an extra form."end ifThe example shows how you can create a more complex Boolean expression with the help of Boolean operators, such as the And operator. The expression
dependents < 9 and audit = falsehas two Boolean expressions as operands (dependents < 9
,audit = false
). If both expressions aretrue
, the value of the entire expression istrue
. Other Boolean operators are Or (another binary operator; if either
of its operands istrue
, the entire expression istrue
), and Not (a unary operator; if its operand istrue
, the expression isfalse
, and vice versa).
For more information about operators, see Chapter 6, "Expressions."